// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import mainManage from "../../framework/mainManage";
import gameData from "../gameData";
import AudioManage, {soundList} from "../../framework/AudioManage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class eggSkin extends cc.Component {

    @property(sp.Skeleton)
    roleNode : sp.Skeleton = null;

    @property(sp.Skeleton)
    eggNode : sp.Skeleton = null;

    @property()
    skinIndex = 0;

    @property()
    showIndex = 0;

    clickTimes = 0;

    onLoad () {
        AudioManage.playSFX(soundList.colour);
        this.eggNode.setAnimation(0,"eggs1",false);
        this.eggNode.setCompleteListener(()=>{
            this.node.getChildByName("mask").active =  true;
            if(this.skinIndex > 8){
                this.roleNode.setSkin("pf" + (this.skinIndex + 1));
            }else{
                this.roleNode.setSkin("pf0" + (this.skinIndex + 1));
            }
        })
    }

    btnClick(event,data){

        if(parseInt(data) == 1){
            gameData.saveRoleSkin(this.skinIndex);
            // gameData.eggSkinList[0] = 1;
            // gameData.saveEggSkinList();
            mainManage.createToast("恭喜解锁新皮肤");
            this.node.destroy();
            if(this.callback != null){
                this.callback(true);
            }
        }

        if(parseInt(data) == 2){
            this.node.destroy();
            if(this.callback != null){
                this.callback(false);
            }
            console.log("不了");
        }
    }

    callback = null;
    setCallBack(skinIndex,orderIndex,callback?){
        this.showIndex = orderIndex;
        this.skinIndex = skinIndex;
        if(callback){
            this.callback = callback;
        }
    }
}
